package jp.veeva.jtsyka.service;

import java.util.ArrayList;
import java.util.List;

import jp.veeva.jtsyka.mappers.OrganizationMapper;
import jp.veeva.jtsyka.mappers.OrganizationRelMapper;
import jp.veeva.jtsyka.model.Organization;
import jp.veeva.jtsyka.model.TreeNodes;
import jp.veeva.jtsyka.model.WhsmstKey;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("OrganizationService")
public class OrganizationServiceImpl implements OrganizationService {

    static final Logger log = LoggerFactory.getLogger(OrganizationServiceImpl.class);

    @Autowired
    private OrganizationMapper organizationMapper;
    @Autowired
    private OrganizationRelMapper organizationRelMapper;

    @Override
    public Organization getOrgByWhsCd(String whsCd) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public TreeNodes getOrgTreeByWhsCd(String whsCd) {
        // TODO Auto-generated method stub
        return null;
    }

    @Transactional
    public List<Organization> listWhsCompany(String customerCd) {
        return organizationMapper.listWhsCompany(customerCd);
    }

    /*
     * (non-Javadoc)
     * @see
     * jp.veeva.jtsyka.service.OrganizationService#listOrganization(java.lang
     * .String)
     */
    @Transactional
    public List<String> listOrganization(WhsmstKey whsmstKey) {
        List<String> list = organizationMapper.listOrganization(whsmstKey);
        return list;
    }

    /*
     * (non-Javadoc)
     * @see jp.veeva.jtsyka.service.OrganizationService#listOrganizationRel()
     */
    @Transactional
    public List<String> listOrganizationRel(String whsCd,String id) {
        int one = 1;
        int zero = 0;
        List<String> list = new ArrayList<String>();
        
        if (null == id || id.equals("")) {
            list = organizationRelMapper.listOrgNodeRel(whsCd, one);
            list.addAll(organizationRelMapper.listOrgNodeRel2(whsCd, one, zero));
        } else {
            list = organizationRelMapper.listOrgNodeRel2(whsCd, one, zero);
        }
        return list;
    }

    /*
     * (non-Javadoc)
     * @see jp.veeva.jtsyka.service.OrganizationService#listOrganizationRel()
     */
    @Transactional
    public List<String> refreshOrganizationTree(String whsCd, String id) {
        //Tree NO は0以外の子供treeを取得する。
        int treeNo = 0;
        List<String> list = organizationRelMapper.getListOrgNodeRel(whsCd, id, treeNo);
        return list;
    }

    /*
     * (non-Javadoc)
     * @see jp.veeva.jtsyka.service.OrganizationService#addOrganization(jp.veeva.jtsyka.model.Organization)
     */
    @Transactional
    public boolean addOrganization(Organization organization) {
        boolean result = false;
        //組織マスターに組織を追加する。
        
        //組織リレーションを作成する。
        
        return result;
    }

}
